/**
 * 业务模块
 */
// const data = require('./data.json');
const path = require('path');
const fs = require('fs');
const db = require('./db');

let maxBookCode = () => {
    let arr = [];
    data.forEach((item) => {
        arr.push(item.id);
    });
    return Math.max.apply(null, arr);
};

// 主页面
exports.showIndex = (req, resp) => {
    // resp.render('index', {list: data});
    let sql = "SELECT * FROM t_book";
    let data = null;
    db.base(sql, data, (err, results, fields)=>{
        if (err) {
            // throw err;
            resp.set('Content-Type', 'text/json; charset=utf8');
            resp.send('系统错误，请联系管理员');
            return;
        }
        resp.render('index', {list: results});
    });
}

// 跳转到“添加图书”的页面
exports.toAddBook = (req,resp) => {
    resp.render('addBook', {});
}
// 添加图书（保存数据）
exports.addBook = (req,resp) => {
    let info = req.body;
    let book = Object.assign({}, info);

    let sql = "INSERT INTO t_book SET ?";
    db.base(sql, book, (err, results, fields)=>{
        if (err) {
            // throw err;
            resp.set('Content-Type', 'text/json; charset=utf8');
            resp.send('系统错误，请联系管理员');
            return;
        }
        if (results && results.affectedRows === 1) {
            resp.redirect('/');
        } else {
            resp.set('Content-Type', 'text/json; charset=utf8');
            resp.send('系统错误，请联系管理员');
        }
    });
}

exports.toEditBook = (req, resp) => {
    // (API) : http://www.expressjs.com.cn/4x/api.html#req.query
    let _id = req.query.id;

    let sql = "SELECT * FROM t_book WHERE id=?";
    db.base(sql, _id, (err, results, fields)=>{
        if (err) {
            // throw err;
            resp.set('Content-Type', 'text/json; charset=utf8');
            resp.send('系统错误，请联系管理员');
            return;
        }
        if (results && results[0].name) {
            resp.render('editBook', {book: results[0]});
        } else {
            resp.set('Content-Type', 'text/json; charset=utf8');
            resp.send('系统错误，请联系管理员');
        }
    });
}

exports.editBook = (req, resp) => {

    let book = req.body;
    let dataArr = [];
    dataArr.push(book.name);
    dataArr.push(book.author);
    dataArr.push(book.category);
    dataArr.push(book.description);
    dataArr.push(book.id);

    // 这里不能这样写,要添加配置。但那个配置可能影响“全局”，如果只是“局部”生效，还可以用一下。
    // let sql = "UPDATE t_book SET name = :name, author = :author, category = :category, description = :description where id = :id";
    let sql = "UPDATE t_book SET name = ?, author = ?, category = ?, description = ? where id = ?";
    db.base(sql, dataArr, (err, results, fields)=>{
        if (err) {
            // throw err;
            console.log(err.stack);
            resp.set('Content-Type', 'text/json; charset=utf8');
            resp.send('系统错误，请联系管理员');
            return;
        }
        if (results && results.affectedRows === 1) {
            resp.redirect('/');
        } else {
            resp.set('Content-Type', 'text/json; charset=utf8');
            resp.send('系统错误，请联系管理员');
        }
    });
}

exports.delBook = (req, resp) => {
    let _id = req.query.id;

    let sql = "DELETE FROM t_book WHERE id=?";
    db.base(sql, _id, (err, results, fields)=>{
        if (err) {
            // throw err;
            resp.set('Content-Type', 'text/json; charset=utf8');
            resp.send('系统错误，请联系管理员');
            return;
        }
        if (results && results.affectedRows === 1) {
            resp.redirect('/');
        } else {
            resp.set('Content-Type', 'text/json; charset=utf8');
            resp.send('系统错误，请联系管理员');
        }
    });
}